<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\IndexModule;
use app\base\model\Coupons;
use app\base\model\Shipment;
class Module extends Common{
    //模块模型
    protected $module;
    //优惠券模型
    protected $coupons;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->module = new IndexModule();
        $this->coupons = new Coupons();
    }
   /**
    *模块banner图列表
    *return json
    */
  public function bannerList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $bannerList = $this->module->where(['type'=>['<>',0]])->select();
      $msg['message']=checkMsg(GET_DATA_SUCCESS);
      $msg['banner_list'] = $bannerList;
      return $msg;
  }
  /**
   *上传banner图
   * return json
   */
  public function uploadBanner(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $type = $this->request->post('type'); //1:今日特购 2:热门商品 3:大师推荐 4:签到领券
      $checkData=array(
          'size'  =>'15678000',
          'ext'   =>'jpg,png,gif'
      );
      if(empty($type)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $result = upload($checkData);
      if($result['code']==1){
          $path = $result['path'];
          $res=$this->module->save(['banner_img'=>$path],['type'=>$type]);
          if($res){
              $msg['message']=checkMsg(UPLOAD_SUCCESS);
          }else{
              $msg['message']=checkMsg(UPLOAD_FAIL);
          }
      }else{
          $msg['message']=checkMsg(UPLOAD_FAIL);
      }
     return $msg;
  }
  /**
   *添加优惠券
   * return json
   */
  public function addCoupon(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $amount = $this->request->post('amount');
      $yc_coin = $this->request->post('yc_coin');
      $cond_amount = $this->request->post('cond_amount',0);
      $image_path = $this->request->post('image_path');
      $start_time = $this->request->post('start_time');
      $end_time   = $this->request->post('end_time');
      if(empty($amount) ||empty($yc_coin) || empty($cond_amount)||empty($image_path) || empty($start_time) || empty($end_time)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $data=[
          'amount'  =>$amount,
          'cond_amount' =>$cond_amount,
          'yc_coin'     =>$yc_coin,
          'img_path'    =>$image_path,
          'start_time'  =>$start_time,
          'end_time'    =>$end_time,
          'add_time'    =>time()
      ];
     $res = $this->coupons->insertGetId($data);
      if($res){
          $msg['message']=checkMsg(ADD_SUCCESS);
      }else{
          $msg['message']=checkMsg(ADD_FAIL);
      }
      return $msg;
  }
  /**
   *优惠券列表
   * return json
   */
  public function couponList(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $list = $this->coupons->where(['is_delete'=>0])->select();
       if(empty($list)){
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }else{
           foreach($list as $key=>$value){
               $end_time=strtotime($value['end_time']." 23:59:59");
               if($end_time>time()){
                   $list[$key]['is_exprise']=0;
               }else{
                   $list[$key]['is_exprise']=1;
               }
           }
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']=$list;
       }
      return $msg;
  }
  /**
   *删除优惠券
   * return json
   */
  public function deleteCoupon(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $id = $this->request->post('c_id');
      if(empty($id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
     $res = $this->coupons->save(['is_delete'=>1],['c_id'=>$id]);
      if($res){
          $msg['message']=checkMsg(DELETE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(DELETE_DATA_FAIL);
      }
      return $msg;
  }
  /**
   *每日一运内容
   * return json
   */
  public function dailyOperation(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $shipment_model = new Shipment();
      $content = $shipment_model->where(['id'=>1])->find();
      if(empty($content)){
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }else{
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['content']=$content;
      }
     return $msg;
  }
  /**
   *修改每日一运的内容
   * return json
   */
  public function editeDaily(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $content = $this->request->post('content');
      if(empty($content)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $shipment_model = new Shipment();
      $res = $shipment_model->save(['content'=>$content],['id'=>1]);
      if($res){
          $msg['message']=checkMsg(UPDATE_DATA_SUCCESS);
      }else{
          $msg['message']=checkMsg(UPDATE_DATA_FAIL);
      }
      return $msg;
  }
}